package prefuse.data.parser;

import java.awt.Font;
import java.util.regex.Pattern;

import prefuse.util.FontLib;

//WEENY added
public class FontParser implements DataParser{
	public boolean canParse(String text) {
		try {
			parse(text);
			return true;
		}
		catch (Exception e) {
		}
		return false;
	}

	public static final String SIZE = ":size=";
	public static final String STYLE = ":style=";
	static Pattern split = Pattern.compile("(?:"+SIZE+")|(?:"+STYLE+")");
	public String format(Object value) {
		if (value == null) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		Font font = (Font) value;
		sb.append(font.getName());
		sb.append(SIZE);
		sb.append(font.getSize());
		sb.append(STYLE);
		sb.append(font.getStyle());
		return sb.toString();
	}

	public Class getType() {
		return Font.class;
	}

	public Object parse(String text) throws DataParseException {
		String[] tokens = split.split(text);
		if (tokens.length == 3) {
			int size = Integer.parseInt(tokens[1].trim());
			int style = Integer.parseInt(tokens[2].trim());
			return FontLib.getFont(tokens[0], style, size);
		}
		throw new DataParseException(text+" does not match "+split.pattern());
	}
	public boolean compatible(Class type) {
		return type == Font.class;
	}
}
